package com.grandbroad.eim.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.grandbroad.eim.R;
import com.grandbroad.eim.ui.activities.VideoActivity;
import com.grandbroad.eim.utils.UiUtlis;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import dev.utils.app.AppCommonUtils;

public class BannerAdapter extends PagerAdapter {
    private Context mContext;
    private List<VodBean> imgIds;
    private BannerViewHolder viewHolder;
    private AppCompatActivity activity;
    public BannerAdapter(Context context, AppCompatActivity activity) {
        this.mContext = context;
        this.activity = activity;
    }

    public void setImgIds(List<VodBean> imgIds) {
        this.imgIds = imgIds;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (imgIds == null || imgIds.size() < 1) {
            return 0;
        }

        return Integer.MAX_VALUE;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.imageview_item, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imgview_item);
        if (imgIds.size()>0)
        {
            VodBean vodBean = imgIds.get(position%imgIds.size());
            // 此处必须将position对imgIds.length取余数，不然基本上会出现数组越界的错误
            x.image().bind(imageView,vodBean.getCover_img());
            //跳转至播放
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UiUtlis.startAnimActivity(activity,VideoActivity.class,v,"screen_img","bean",vodBean,true);
                }
            });
        }
        container.addView(view);

        return view;
    }

    class BannerViewHolder
    {
        private View view;
        private ImageView imageView;
        public BannerViewHolder(View view)
        {
            this.view =view;
            ImageView imageView = (ImageView) view.findViewById(R.id.imgview_item);
        }

        public View getView() {
            return view;
        }

        public ImageView getImageView() {
            return imageView;
        }
    }
}
